package net.toocruel.iqismart.web.controller.core;

import net.toocruel.iqismart.entity.common.Category;
import net.toocruel.iqismart.entity.constants.CategoryVisibility;
import net.toocruel.iqismart.entity.core.Comment;
import net.toocruel.iqismart.entity.core.Question;
import net.toocruel.iqismart.entity.rbac.User;
import net.toocruel.iqismart.service.UserService;
import net.toocruel.iqismart.service.core.CategoryService;
import net.toocruel.iqismart.service.core.CommentService;
import net.toocruel.iqismart.service.core.QuestionService;
import net.toocruel.iqismart.service.core.TagService;
import net.toocruel.iqismart.web.controller.base.AbstractBaseController;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("comment")
public class CommentController extends AbstractBaseController {

    @Autowired
    private UserService userService;

    @Autowired
    private TagService tagService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private QuestionService questionService;

    @Autowired
    private CommentService commentService;

    @GetMapping("create")
    public String create(Long toUserId,HttpServletRequest request,Question question){
        if(toUserId!=null && toUserId>0){
            User toUser = userService.findById(toUserId);
            request.setAttribute("toUser",toUser);
        }

        List<Category> categorys = categoryService.findByVisibilityAndRoles(CategoryVisibility.questions,getCurrentUser());

        request.setAttribute("categorys",categorys);

        return getTemplate("create");
    }

    @PostMapping("store")
    public ModelAndView store(@Valid Comment comment, BindingResult bindingResult,
                        HttpServletRequest request, HttpServletResponse response){
        List<FieldError> allErrors = new ArrayList<>();
        bindingResult.getFieldErrors().forEach(fieldError -> {
            if(!fieldError.getField().equals("toUser")){
                allErrors.add(fieldError);
            }
        });

        if(CollectionUtils.isEmpty(allErrors)){
            try {
                //发布评论
                commentService.postComment(comment);
            } catch (Exception e) {
                e.printStackTrace();
                request.setAttribute("error",e);
            }
        }
        return new ModelAndView(getTemplate("item"),"comment",comment);
    }

    @GetMapping("{id}")
    public ModelAndView detail(@PathVariable Long id, HttpServletRequest request){

        Question question = questionService.findById(id,getCurrentUser());

        return new ModelAndView(getTemplate("detail"),"question",question);
    }

    @Override
    protected String getModule() {
        return "comment";
    }
}
